//
//  CJRECMainTableViewCell.m
//  CoJellyCartoon
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJRECMainTableViewCell.h"
#import "CJRECMainCollectionViewCell.h"
@interface CJRECMainTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collecitonview;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowlayout;
@end
@implementation CJRECMainTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatCollectionView];
    }
    return self;
}

-(void)creatCollectionView{
    self.flowlayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowlayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    self.collecitonview = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowlayout];
    self.collecitonview.dataSource = self;
    self.collecitonview.delegate =self;
    self.collecitonview.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1];
    [self.collecitonview registerClass:[CJRECMainCollectionViewCell class] forCellWithReuseIdentifier:@"CJRECMainCollectionViewCell"];
    [self.contentView addSubview:self.collecitonview];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.flowlayout.itemSize = CGSizeMake(((self.contentView.frame.size.width - 40) / 2), ((self.contentView.frame.size.height -20) / 2));
    self.flowlayout.minimumInteritemSpacing = 5;
    self.flowlayout.minimumLineSpacing = 10;
    self.flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    self.collecitonview.frame = self.bounds;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.recDataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CJRECMainCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CJRECMainCollectionViewCell" forIndexPath:indexPath];
    
    item.RecBodyModel = self.recDataArray[indexPath.item];
    
    return item;
}
-(void)setRecDataArray:(NSArray *)recDataArray{
    _recDataArray = recDataArray;
    [self.collecitonview reloadData];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSNotification *notification = [NSNotification notificationWithName:@"newsinfo" object:nil userInfo:@{@"model": self.recDataArray[indexPath.item]}];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}
@end
